<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class Message extends Base
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_message';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'content_text',
        'createtime_text',
        'type_text',
    ];

    public function getContentTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['content']) ? $data['content'] : '');
        return $value ? strip_tags($value) : '';
    }

    public function getTypeList()
    {
        return ['1' => __('系统通知'), '2' => __('房源通知'), '3' => __('每日推送'), '4' => __('签约消息')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getHouseDataAttr($value, $data)
    {
        $house_ids = $data['house_ids'];
        $house_data = [];
        if ($house_ids) {
            $house_data = House::field(['id', 'name', 'image'])->where('id', 'in', $house_ids)->select();
        }
        return $house_data;
    }

    public function user()
    {
        return $this->belongsTo('User', 'to_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function house()
    {
        return $this->belongsTo('House', 'house_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
